<?php

class WBBThreadWebsiteAdapter extends WebsiteAdapter {
	private $boardID = 0;
	private $params = array();
	
	public function matches($url) {
		$this->params = WebsiteUtil::getParams($url);
		return (array_key_exists("page", $this->params) && strtolower($this->params['page']) == "thread");
	}
	
	public function parse(Website $website) {
		$board = WebsiteUtil::getBoardByUrl($website->getURL());
		$this->boardID = $board->id;
		phpQuery::newDocumentHTML($website->getContent());
		$elem = pq("div[class=headlineContainer] h2");
		$threadID = intval(str_replace("threadTitle", "", pq($elem)->attr("id")));
		$archive = new ArchiveBoardPost();
		
		foreach(pq("div[id*=postRow]") as $container) {
			$archive->unsetAttributes();
			$container = pq($container);
			$id = intval(str_replace("postRow", "", $container->attr("id")));
			$text = $container->find("[id=postText".$id."]")->text();
			$title = $container->find("[id=postTopic".$id."] span")->text();
			$boardID = $this->boardID;
			$params = WebsiteUtil::getParams(pq($container->find("div.messageAuthor p.userName a"))->attr("href"));
			$userID = (array_key_exists("userID", $params)) ? intval($params['userID']) : 0;
			
			//Erstelle
			$archive->postID = $id;
			$archive->text = $text;
			$archive->title = $title;
			$archive->userID = $userID;
			
			//Suche
			$found = $archive->find("postID = '".$id."'");
			if ($found != null) {
				if (strlen($title) > 0) $found->title = $title;
				if (strlen($text) > 0) $found->text = $text;
				if ($userID != 0) $found->userID = $userID;
				$found->update();
			}else{
				$archive->setIsNewRecord(true);
				$archive->insert();
				
	//			echo "Post erstellt: ".$boardID.".".$threadID.".".$id." ".$title."<br>";
			}
			
			foreach(pq(".contentHeader .pageNavigation li[class!=skip]") as $container) {
				$link = pq($container)->find("a");
				WebsiteUtil::addURL(pq($link)->attr("href"), false, $board);
			}
		}
	}
}
?>
